/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.UnitDao;
import com.tesam.gedeon.entity.Unit;
import com.tesam.gedeon.service.UnitService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sistema
 */
@Service
public class UnitServiceImpl implements UnitService {

    @Autowired
    UnitDao unitDao;
    
    @Override
    public int getExistCode(Unit unit){
        return unitDao.getExistCode(unit);
    }
    
    @Override
    public int getCountAll() {
        return unitDao.getCountAll();
    }

    @Override
    public int getCountBySearch(Unit unit) {
        return unitDao.getCountBySearch(unit);
    }

    @Override
    public List<Unit> getListByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
        return unitDao.getListByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }

    @Override
    public List<Unit> getListBySearch(int startLimit, int endLimit, String dataToSort, String directionOfOrder, Unit unit) {
        return unitDao.getListBySearch(startLimit, endLimit, dataToSort, directionOfOrder, unit);
    }

    @Override
    public void delete(Unit unit) {
         unitDao.delete(unit);
    }

    @Override
    public int saveUnit(Unit unit) {
        return unitDao.saveUnit(unit);
    }

    @Override
    public void update(Unit unit) {
       unitDao.update(unit);
    }
    
    @Override
    public List<Unit> getUnitById(Unit unit){
        return unitDao.getUnitById(unit);
    }
    
    @Override
    public String getJsonDataFromArrayList(List<Unit> listUnit, int quantityRecord) {

        JSONArray array = null;
        JSONObject json = null;
        String deleteButton = "" ;
        String updateButton = "" ;
        String viewButton = "" ;
        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listUnit.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");
              
                for (Unit unit : listUnit) {
                    JSONArray arrayPut = new JSONArray();
 
                    viewButton = "<a href='view?id="+unit.getId()+"&module=14' ";
                    viewButton+= "class='btn mini blue'><i class='icon-share'></i> View </a>&nbsp;";
                    
                    updateButton = "<a href='update?id="+unit.getId()+"&module=14' class='btn mini green' >" ;
                    updateButton+= "<i class='icon-pencil'></i> Update</a>&nbsp;";
                    
                    deleteButton = "<a class='btn mini red' data-toggle='modal' ";
                    deleteButton+= "onclick=\"";     
                    deleteButton+="confirmDelete('"+unit.getId()+"')";
                    deleteButton+="\" "; 
                    deleteButton+="href='#static' >";
                    deleteButton+="<i class='icon-trash'></i> Delete</a>";      
                        
                    arrayPut.put(unit.getCode()); 
                    arrayPut.put(StringEscapeUtils.escapeHtml4(unit.getName()));
                    arrayPut.put(viewButton+updateButton+deleteButton);
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", quantityRecord);
            json.put("iTotalDisplayRecords", quantityRecord);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }

    
}
